package com.woniu.custom.controller;

import cn.hutool.core.util.ObjectUtil;
import com.woniu.common.dto.UserDTO;
import com.woniu.common.entity.AddrDto;
import com.woniu.common.utils.ResponseData;
import com.woniu.custom.pojo.Addr;
import com.woniu.custom.pojo.ZyUser;
import com.woniu.custom.service.AddrService;
import com.woniu.custom.service.ZyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ltyipx
 * @version 1.0
 * @description:TODO
 * @date 2024/8/8 10:31
 */
@RestController
public class userController {
    @Autowired
    private ZyUserService userService;
    @Autowired
    private AddrService addrService;
    @PostMapping("/login")
    public ResponseData login(@RequestBody ZyUser user){
        return userService.login(user);
    }

    @PostMapping("/register")
    public ResponseData register(@RequestBody ZyUser user){
        return userService.register(user);
    }

    @GetMapping("/isLogin")
    public ResponseData isLogin(){
        return userService.isLogin();
    }
    @GetMapping("/queryUserById")
    public ResponseData queryUserById(@RequestParam("id") String id){
        ZyUser user = userService.getById(id);
        return new ResponseData().success(user);
    }


    //售后退款时，给zyUser表中的用户账户余额加钱
    @PostMapping("/UpdateUser")
    public ResponseData<?> UpdateUser(@RequestBody UserDTO userDTO){
        String id = userDTO.getId();
        ZyUser user = userService.getById(id);
        //传过来的userDto里面的账户余额已经是退款后的
        if(!ObjectUtil.isEmpty(userDTO.getUserPurse())){
            user.setUserPurse(userDTO.getUserPurse());
        }
        user.setUserConsumeAmount(userDTO.getUserConsumeAmount());
        boolean b = userService.updateById(user);
        if (b) {
            return new ResponseData<>().success();
        }else {
            return new ResponseData<>().fail();
        }
    }

    /**
     * 添加售后表时查订单详情的地址
     */
    @PostMapping("/queryAddrByAddrId")
    public AddrDto queryAddrByAddrId(String addrId){
        Addr addr = addrService.getById(addrId);
        AddrDto addrDto = new AddrDto();
        addrDto.setId(addr.getId());
        addrDto.setUserId(addr.getUserId());
        addrDto.setDetail(addr.getDetail());
        addrDto.setCreateTime(addr.getCreateTime());
        addrDto.setProvinceId(addr.getProvinceId());
        addrDto.setDefaultAddress(addrDto.getDefaultAddress());
        addrDto.setStatus(addrDto.getStatus());
        addrDto.setAddressTel(addr.getAddressTel());
        addrDto.setAddressName(addr.getAddressName());
        addrDto.setProvince(addr.getProvince());
        addrDto.setCity(addr.getCity());
        addrDto.setCounty(addr.getCounty());
        addrDto.setLatitude(addr.getLatitude());
        addrDto.setLongitude(addr.getLongitude());
        return addrDto;

    }
}
